package cn.iocoder.yudao.module.wine.controller.admin.stockinfoitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.stockinfoitem.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.stockinfoitem.StockInfoitemDO;
import cn.iocoder.yudao.module.wine.service.stockinfoitem.StockInfoitemService;

@Tag(name = "管理后台 - 出入库信息项")
@RestController
@RequestMapping("/wine/stock-infoitem")
@Validated
public class StockInfoitemController {

    @Resource
    private StockInfoitemService stockInfoitemService;

    @PostMapping("/create")
    @Operation(summary = "创建出入库信息项")
    @PreAuthorize("@ss.hasPermission('wine:stock-infoitem:create')")
    public CommonResult<Long> createStockInfoitem(@Valid @RequestBody StockInfoitemSaveReqVO createReqVO) {
        return success(stockInfoitemService.createStockInfoitem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出入库信息项")
    @PreAuthorize("@ss.hasPermission('wine:stock-infoitem:update')")
    public CommonResult<Boolean> updateStockInfoitem(@Valid @RequestBody StockInfoitemSaveReqVO updateReqVO) {
        stockInfoitemService.updateStockInfoitem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出入库信息项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:stock-infoitem:delete')")
    public CommonResult<Boolean> deleteStockInfoitem(@RequestParam("id") Long id) {
        stockInfoitemService.deleteStockInfoitem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出入库信息项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:stock-infoitem:query')")
    public CommonResult<StockInfoitemRespVO> getStockInfoitem(@RequestParam("id") Long id) {
        StockInfoitemDO stockInfoitem = stockInfoitemService.getStockInfoitem(id);
        return success(BeanUtils.toBean(stockInfoitem, StockInfoitemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出入库信息项分页")
    @PreAuthorize("@ss.hasPermission('wine:stock-infoitem:query')")
    public CommonResult<PageResult<StockInfoitemRespVO>> getStockInfoitemPage(@Valid StockInfoitemPageReqVO pageReqVO) {
        PageResult<StockInfoitemDO> pageResult = stockInfoitemService.getStockInfoitemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockInfoitemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出入库信息项 Excel")
    @PreAuthorize("@ss.hasPermission('wine:stock-infoitem:export')")
    @OperateLog(type = EXPORT)
    public void exportStockInfoitemExcel(@Valid StockInfoitemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockInfoitemDO> list = stockInfoitemService.getStockInfoitemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出入库信息项.xls", "数据", StockInfoitemRespVO.class,
                        BeanUtils.toBean(list, StockInfoitemRespVO.class));
    }

}
